﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace SharpShare.Configuration {
    public class ServiceSection<TServiceElement> : ConfigurationSection, IExtensionConfigurationSection<ServiceElement> where TServiceElement : ServiceElement, new() {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public GenericElementCollection<TServiceElement> Services {
            get {
                return (GenericElementCollection<TServiceElement>)this[""];
            }
        }

        #region IExtensionConfigurationSection<ServiceElement> Members

        ServiceElement IExtensionConfigurationSection<ServiceElement>.this[string name] {
            get { return this.Services[name]; }
        }

        void IExtensionConfigurationSection<ServiceElement>.Remove(ServiceElement element) {
            this.Services.Remove(element.Name);
        }

        void IExtensionConfigurationSection<ServiceElement>.Add(ServiceElement element) {
            this.Services.Add((TServiceElement)element);
        }

        #endregion

        #region IExtensionConfigurationSection

        ConfigurationElement IExtensionConfigurationSection.this[string name] {
            get {
                return this.Services[name];
            }
        }

        void IExtensionConfigurationSection.Add(ConfigurationElement element) {
            this.Services.Add((TServiceElement)element);
        }

        void IExtensionConfigurationSection.Remove(ConfigurationElement element) {
            this.Services.Remove(((TServiceElement)element).Name);
        }

        Type IExtensionConfigurationSection.ElementType { get { return typeof(TServiceElement); } }

        #endregion

        #region IEnumerable<ConfigurationElement> Members

        public IEnumerator<ConfigurationElement> GetEnumerator() {
            return this.Services.Cast<ConfigurationElement>().GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        #endregion
    }
}
